package com.example.demo.leetcode.classics150;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _134x的平方根
 * @description
 * @date 2023/08/24 16:56
 * <p>
 * ******************************************************
 */
public class _134x的平方根 {

    public int mySqrt(int x) {
        int left = 0;
        int right = x;
        int ans = 0;
        int mid;
        long res;
        while (left <= right) {
            mid = right - (right - left) / 2;
            res = (long) mid * (long) mid;
            if (res > x) {
                right = mid - 1;
            } else {
                ans = mid;
                left = mid + 1;
            }
        }
        return ans;
    }

}
